package com.information.manage.basic.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.information.manage.basic.common.utils.RedisUtils;
import com.information.manage.basic.mapper.SysRelUserStaffMapper;
import com.information.manage.basic.model.dto.StaffImportDTO;
import com.information.manage.basic.model.dto.StaffImportResultDTO;
import com.information.manage.basic.model.entity.RelStaffDepDO;
import com.information.manage.basic.model.entity.StaffDO;
import com.information.manage.basic.model.entity.StaffDetailDO;
import com.information.manage.basic.model.entity.SysRelUserStaffDO;
import com.information.manage.basic.model.query.StaffAddQuery;
import com.information.manage.basic.model.query.StaffExportQuery;
import com.information.manage.basic.model.query.StaffPageQuery;
import com.information.manage.basic.model.query.StaffUpdateQuery;
import com.information.manage.basic.model.vo.StaffExportVO;
import com.information.manage.basic.model.vo.StaffFindDetailVO;
import com.information.manage.basic.model.vo.StaffNameAndJobNumberVO;
import com.information.manage.basic.model.vo.StaffPageVO;
import com.information.manage.basic.common.constant.Constant;
import com.information.manage.basic.common.recycling.RecoveryDataOperateManager;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.common.utils.FileUtils;
import com.information.manage.basic.common.utils.zip.TooMuchEntryException;
import com.information.manage.basic.common.utils.zip.ZipParser;
import com.information.manage.basic.common.utils.zip.ZipUtils;
import com.information.manage.basic.mapper.RelStaffDepMapper;
import com.information.manage.basic.mapper.StaffDetailMapper;
import com.information.manage.basic.mapper.StaffMapper;
import com.information.manage.basic.service.DepartmentService;
import com.information.manage.basic.service.StaffService;
import com.information.manage.basic.common.utils.exportAndImport.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sy
 * @since 2021-12-29
 */
@Slf4j
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, StaffDO> implements StaffService {

    @Resource
    private FileUtils fileUtils;

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private StaffDetailMapper staffDetailMapper;

    @Resource
    private RecoveryDataOperateManager recoveryDataOperateManager;

    @Resource
    private RelStaffDepMapper relStaffDepMapper;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private SysRelUserStaffMapper sysRelUserStaffMapper;

    //员工导入数量限制
    public static final int IMPORT_MAX_SIZE = 2000;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> addStaff(StaffAddQuery staffAddQuery, MultipartFile image) {
        if (checkStaffJobNumber(null, staffAddQuery.getJobNumber())) return Result.fail("该工号存在未退休的员工");
        if (staffAddQuery.getIdCardNumber() != null && !"".equals(staffAddQuery.getIdCardNumber()) && checkStaffIdCardNumber(null, staffAddQuery.getIdCardNumber())){
            return Result.fail("身份证号已存在");
        }else{
            {// 2022-01-26 gzl 新增 [当身份证为空字符串是置为空]
                String idCardNumber = staffAddQuery.getIdCardNumber();
                if(idCardNumber != null && "".equals(idCardNumber = idCardNumber.trim())){
                    staffAddQuery.setIdCardNumber(null);
                }else{
                    staffAddQuery.setIdCardNumber(idCardNumber);
                }
            }
        }
        //员工详情
        StaffDetailDO staffDetailDO = new StaffDetailDO();
        System.out.println("================>" + staffAddQuery);
        staffDetailDO.setGender(staffAddQuery.getGender());
        staffDetailDO.setTelephone(staffAddQuery.getTelephone());
        staffDetailDO.setJob(staffAddQuery.getJob());
        staffDetailDO.setPoliticsFace(staffAddQuery.getPoliticsFace());
        //设置员工状态 默认在职  todo 在职状态的字典id
        staffDetailDO.setState(0);
        staffDetailDO.setRemark(staffAddQuery.getRemark());
        staffDetailMapper.insert(staffDetailDO);
        //员工基本信息
        StaffDO staffDO = new StaffDO();
        staffDO.setName(staffAddQuery.getName());
        staffDO.setJobNumber(staffAddQuery.getJobNumber());
        staffDO.setIdCardNumber(staffAddQuery.getIdCardNumber());
        staffDO.setType(staffAddQuery.getType());
        try{
            //保存图片并返回图片路径
            if (image != null){
                String imagePath = fileUtils.savePic(image.getInputStream(), "jpg");
                staffDO.setIdCardImage(imagePath);
                //使用人脸识别处理 todo
            }
            //保存指纹数据
            if (false){ //开启指纹录入
                if (staffAddQuery.getFingerprint() != null && !"".equals(staffAddQuery.getFingerprint().trim())){
                    String path = fileUtils.saveBase64(staffAddQuery.getFingerprint());
                    staffDO.setFingerprint(path);
                }
            }
        }catch (Exception e){
            log.info("图片路径、开启人脸识别和指纹录入时数据写入失败");
        }
        staffDO.setCreateTime(LocalDateTime.now());
        staffDO.setStaffDetailId(staffDetailDO.getId());
        StaffDO staffResult = staffMapper.selectOne(new QueryWrapper<StaffDO>().eq("job_number", staffDO.getJobNumber()));
        if (staffResult != null){
            return Result.fail("工号已存在");
        }
        staffMapper.insert(staffDO);
        //保存部门
        RelStaffDepDO relStaffDepDO = new RelStaffDepDO();
        relStaffDepDO.setStaffId(staffDO.getId());
        relStaffDepDO.setDepartmentId(staffAddQuery.getDepartmentId());
        int insert = relStaffDepMapper.insert(relStaffDepDO);
        if (insert != 0) return Result.ok(staffDO.getId());
        else return Result.fail();

    }

    /**
     * 检查员工工号是否存在于未退休的员工上
     * @param staffId 需要排除的员工id
     * @param staffJobNumber 需要检测的员工工号
     * @return true 存在 / false 不存在
     */
    private boolean checkStaffJobNumber(Integer staffId, String staffJobNumber){
        List<Integer> detailIds;
        //获取该工号的所有员工详情id
        if (staffId == null) { //不排除
            detailIds = staffMapper.selectObjs(new LambdaQueryWrapper<StaffDO>()
                    .select(StaffDO::getStaffDetailId).eq(StaffDO::getJobNumber, staffJobNumber)
                    .ne(StaffDO::getId, staffId)).stream().map(it -> (Integer) it).collect(Collectors.toList());
        } else {  //排除某个员工id  用于排除更新时自身
            detailIds = staffMapper.selectObjs(new LambdaQueryWrapper<StaffDO>()
                    .select(StaffDO::getStaffDetailId).eq(StaffDO::getJobNumber, staffJobNumber)
                    .ne(StaffDO::getId, staffId)).stream().map(it -> (Integer) it).collect(Collectors.toList());
        }
        if (detailIds.size() == 0) return false;
        Integer count = staffDetailMapper.selectCount(new LambdaQueryWrapper<StaffDetailDO>()
                .in(StaffDetailDO::getId, detailIds)
                .eq(StaffDetailDO::getState, 0)); //todo 未退休状态字典id
        if (count != 0) return true;
        return false;
    }

    /**
     * 检查员工身份证号是否重复
     * @param staffId 需要排除的员工id
     * @param idCardNumber 需要检测的身份证号
     * @return
     */
    private boolean checkStaffIdCardNumber(Integer staffId, String idCardNumber){
        Integer count;
        if (staffId == null){ //不排除
            count = staffMapper.selectCount(new LambdaQueryWrapper<StaffDO>()
                    .eq(StaffDO::getIdCardNumber, idCardNumber));
        }else {  //排除某个员工进行判断 用于更新时排除自身
            count = staffMapper.selectCount(new LambdaQueryWrapper<StaffDO>()
                    .eq(StaffDO::getIdCardNumber, idCardNumber)
                    .ne(StaffDO::getId, staffId));
        }
        if (count != 0) return true;
        return false;
    }

    @Override
    public Boolean deleteStaff(List<Integer> staffIds) {
        recoveryDataOperateManager.deleteData(StaffDO.class, staffIds);
        return true;
    }

    @Override
    public Result<Page<StaffPageVO>> queryStaffPage(StaffPageQuery staffPageQuery) {
        List<Integer> staffIds;
        if (staffPageQuery.getDepartmentId() != null){
            //获取部门及其子部门下的所有员工id
            List<Integer> depIds = departmentService.getDepartmentAndChildId(staffPageQuery.getDepartmentId());
            staffIds = relStaffDepMapper.selectObjs(new LambdaQueryWrapper<RelStaffDepDO>()
                    .in(RelStaffDepDO::getDepartmentId, depIds)).stream().map(it -> (Integer) it).collect(Collectors.toList());
        }else {
            staffIds = Collections.EMPTY_LIST;
        }
        Page<StaffPageVO> staffPageDTOPage = staffMapper.queryStaffPage(new Page(staffPageQuery.getCurPage(), staffPageQuery.getPageSize())
                , staffPageQuery.getCombination(), staffPageQuery.getFingerprint(), staffIds);
        for (StaffPageVO record : staffPageDTOPage.getRecords()) {
            Integer depId = (Integer) relStaffDepMapper.selectObjs(new LambdaQueryWrapper<RelStaffDepDO>()
                    .select(RelStaffDepDO::getDepartmentId)
                    .eq(RelStaffDepDO::getStaffId, record.getId())).stream().findFirst().orElse(null);
            Map<Integer, String> map = departmentService.generateParentAndChildDepartment(Arrays.asList(depId));
            record.setDepartment(map.get(depId));
            record.setBuildState("未施工");
        }
        return Result.ok(staffPageDTOPage);
    }

    @Override
    public List<StaffNameAndJobNumberVO> queryStaffByCombination(String combination) {
        List<StaffNameAndJobNumberVO> staffNameAndJobNumberVOS = staffMapper.queryStaffByCombination(combination);
        return staffNameAndJobNumberVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> updateStaff(StaffUpdateQuery staffUpdateQuery, MultipartFile image) {
        if (checkStaffJobNumber(staffUpdateQuery.getStaffId(), staffUpdateQuery.getJobNumber())) return Result.fail("该工号存在未退休的员工");
        if (staffUpdateQuery.getIdCardNumber() != null && !"".equals(staffUpdateQuery.getIdCardNumber()) && checkStaffIdCardNumber(staffUpdateQuery.getStaffId(), staffUpdateQuery.getIdCardNumber())){
            return Result.fail("身份证号已存在");
        }else{
            {// 2022-01-26 gzl 新增 [当身份证为空字符串是置为空]
                String idCardNumber = staffUpdateQuery.getIdCardNumber();
                if(idCardNumber != null && "".equals(idCardNumber = idCardNumber.trim())){
                    staffUpdateQuery.setIdCardNumber(null);
                }else{
                    staffUpdateQuery.setIdCardNumber(idCardNumber);
                }
            }
        }
        //获取详情id
        StaffDO old = staffMapper.selectOne(new LambdaQueryWrapper<StaffDO>().eq(StaffDO::getId, staffUpdateQuery.getStaffId()));
        if (old.getStaffDetailId() == null) return Result.ok(false);
        //修改基本信息表
        StaffDO staffDO = new StaffDO();
        staffDO.setId(staffUpdateQuery.getStaffId());
        staffDO.setName(staffUpdateQuery.getName());
        staffDO.setJobNumber(staffUpdateQuery.getJobNumber());
        staffDO.setIdCardNumber(staffUpdateQuery.getIdCardNumber());
        staffDO.setType(staffUpdateQuery.getType());
        if (image != null){ //保存图片并返回图片路径
            String imagePath = null;
            try {
                imagePath = fileUtils.savePic(image.getInputStream(), "jpg");
            } catch (IOException e) {
                e.printStackTrace();
            }
            staffDO.setIdCardImage(imagePath);
            if (false){ //开启人脸识别
//                    String eigenvalues = organizationProvider.getEigenvalues(imagePath);
                //特征值路径
//                    String eigenvaluesPath = organizationProvider.saveEigenvalues(eigenvalues);
//                    staffDO.setEigenvalues(eigenvaluesPath);
            }
            // 删除原有的图片
//                organizationProvider.deleteStaffImage(old.getIdCardImage());
        }
        staffDO.setUpdateTime(LocalDateTime.now());
        staffMapper.updateById(staffDO);
        //修改详情
        StaffDetailDO staffDetailDO = new StaffDetailDO();
        staffDetailDO.setId(old.getStaffDetailId());
        staffDetailDO.setGender(staffUpdateQuery.getGender());
        staffDetailDO.setTelephone(staffUpdateQuery.getTelephone());
        staffDetailDO.setJob(staffUpdateQuery.getJob());
        staffDetailDO.setPoliticsFace(staffUpdateQuery.getPoliticsFace());
        staffDetailDO.setRemark(staffUpdateQuery.getRemark());
        staffDetailMapper.updateById(staffDetailDO);
        //修改员工部门中间表
        //更改员工的所在部门
        Integer staffId = staffUpdateQuery.getStaffId();
        Integer departmentId = staffUpdateQuery.getDepartmentId();
        Integer count = relStaffDepMapper.selectCount(new LambdaQueryWrapper<RelStaffDepDO>()
                .eq(RelStaffDepDO::getStaffId, staffId));
        int updateOrInsert = 0;
        if (count > 0){ //存在 更新
            updateOrInsert = relStaffDepMapper.update(null, new LambdaUpdateWrapper<RelStaffDepDO>()
                    .set(RelStaffDepDO::getDepartmentId, departmentId)
                    .eq(RelStaffDepDO::getStaffId, staffId));
        }else { //不存在 添加
            RelStaffDepDO relStaffDepDO = new RelStaffDepDO();
            relStaffDepDO.setStaffId(staffId);
            relStaffDepDO.setDepartmentId(departmentId);
            updateOrInsert = relStaffDepMapper.insert(relStaffDepDO);
        }
        if (updateOrInsert == 0) return Result.ok(false);
        List<SysRelUserStaffDO> sysRelUserStaffDOS = sysRelUserStaffMapper.selectList(new LambdaQueryWrapper<SysRelUserStaffDO>().eq(SysRelUserStaffDO::getStaffId, staffId));
        if (CollectionUtils.isNotEmpty(sysRelUserStaffDOS)) {
            for (SysRelUserStaffDO sysRelUserStaffDO : sysRelUserStaffDOS) {
                redisUtils.remove(Constant.userHotDataKeyPrex + sysRelUserStaffDO.getUserId());
            }
        }
        return Result.ok(true);
    }

    @Override
    public Result<StaffFindDetailVO> detailStaff(Integer staffId) {
        StaffFindDetailVO staffFindDetailVO = new StaffFindDetailVO();
        StaffDO staffDO = staffMapper.selectOne(new LambdaQueryWrapper<StaffDO>().eq(StaffDO::getId, staffId));
        if (staffDO == null) return Result.fail("该员工不存在");
        //设置基本信息
        staffFindDetailVO.setId(staffId);
        staffFindDetailVO.setStaffName(staffDO.getName());
        staffFindDetailVO.setJobNumber(staffDO.getJobNumber());
        staffFindDetailVO.setIdCardNumber(staffDO.getIdCardNumber());
        staffFindDetailVO.setType(staffDO.getType());
        staffFindDetailVO.setFingerprint(staffDO.getFingerprint() == null || "".equals(staffDO.getFingerprint().trim()) ? false : true);
        staffFindDetailVO.setImagePath(staffDO.getIdCardImage() == null || "".equals(staffDO.getIdCardImage().trim()) ? "" : staffDO.getIdCardImage());
        //设置详情
        if (staffDO.getStaffDetailId() != null){
            StaffDetailDO staffDetailDO = staffDetailMapper.selectOne(new LambdaQueryWrapper<StaffDetailDO>().eq(StaffDetailDO::getId, staffDO.getStaffDetailId()));
            staffFindDetailVO.setJob(staffDetailDO.getJob());
            staffFindDetailVO.setTelephone(staffDetailDO.getTelephone());
            staffFindDetailVO.setState(staffDetailDO.getState());
            staffFindDetailVO.setPoliticsFace(staffDetailDO.getPoliticsFace());
            staffFindDetailVO.setRemark(staffDetailDO.getRemark());
            staffFindDetailVO.setGender(staffDetailDO.getGender());
        }
        //设置部门
        Integer depId = (Integer) relStaffDepMapper.selectObjs(new LambdaQueryWrapper<RelStaffDepDO>()
                .select(RelStaffDepDO::getDepartmentId)
                .eq(RelStaffDepDO::getStaffId, staffId)).stream().findFirst().orElse(null);
        Map<Integer, String> map = departmentService.generateParentAndChildDepartment(Arrays.asList(depId));
        String department = map.get(depId);
        staffFindDetailVO.setDepartmentId(depId);
        staffFindDetailVO.setDepartment(department);
        return Result.ok(staffFindDetailVO);
    }

    @Override
    public Boolean retireStaff(List<Integer> staffIds) {
        //获取退休的所有员工信息
        List<StaffDO> staffDOList = staffMapper.selectList(new LambdaQueryWrapper<StaffDO>().in(StaffDO::getId, staffIds));
        //获取所有的员工详情id
        List<Integer> detailIds = staffDOList.stream().map(it -> it.getStaffDetailId()).collect(Collectors.toList());
        //修改状态及退休时间
        staffDetailMapper.update(null, new LambdaUpdateWrapper<StaffDetailDO>()
                .in(StaffDetailDO::getId, detailIds)
                .set(StaffDetailDO::getState, 1)  //todo 退休状态字典id
                .set(StaffDetailDO::getRetireTime, LocalDateTime.now()));
        return true;
    }

    @Override
    public Boolean fingerPrintInput(Integer staffId, String fingerprint) {
        //获取已有的指纹数据存放地址
        StaffDO staffDO = staffMapper.selectOne(new LambdaQueryWrapper<StaffDO>().eq(StaffDO::getId, staffId));
        //先添加新的指纹数据再删除
        //保存指纹数据并获取存储位置
//        String fingerprintPath = organizationProvider.saveFingerprint(fingerprint);
        //修改库中存放的指纹数据文件的路径
//        staffMapper.update(null, new LambdaUpdateWrapper<StaffDO>()
//                .eq(StaffDO::getId, staffId)
//                .set(StaffDO::getFingerprint, fingerprintPath));
//        if (staffDO.getFingerprint() != null && !"".equals(staffDO.getFingerprint().trim())){ //将已存在的指纹数据删除
//            organizationProvider.deleteFingerprint(staffDO.getFingerprint());
//        }
        return true;
    }

    @Override
    public void deleteFingerprint(Integer staffId) {
        StaffDO staffDO = staffMapper.selectOne(new LambdaQueryWrapper<StaffDO>().eq(StaffDO::getId, staffId));
        if (staffDO.getFingerprint() == null || "".equals(staffDO.getFingerprint().trim()))
            throw new RuntimeException("员工id:" + staffId + "指纹数据不存在");
        //移除 指纹文件数据
//        organizationProvider.deleteFingerprint(staffDO.getFingerprint());
        //移除路径
        staffMapper.update(null, new LambdaUpdateWrapper<StaffDO>()
        .set(StaffDO::getFingerprint, null)
        .eq(StaffDO::getId, staffId));
    }

    @Override
    public void excelExport(HttpServletResponse response, StaffExportQuery staffExportQuery) {
        List<Integer> staffIds = new ArrayList<>();
        if (staffExportQuery.getDepartmentId() != null){
            //获取当前部门及子部门id
            List<Integer> depIds = departmentService.getDepartmentAndChildId(staffExportQuery.getDepartmentId());
            //获取部门下所有的员工id
            staffIds = relStaffDepMapper.selectObjs(new LambdaQueryWrapper<RelStaffDepDO>()
                    .in(RelStaffDepDO::getDepartmentId, depIds)).stream().map(it -> (Integer) it).collect(Collectors.toList());
        }else {
            staffIds = Collections.EMPTY_LIST;
        }
        List<StaffExportVO> staffExportVOS = staffMapper.queryStaffExportList(staffExportQuery.getCombination(), staffExportQuery.isFingerprint(), staffIds);
        ExcelUtils.exportExcel(staffExportVOS, null, "sheet1", StaffExportVO.class, "员工导出.xls", response);
    }

    @Override
    public void downloadExcel(HttpServletResponse response) {
        ZipUtils.downloadZipTemplate(response, "/static/basic/staff.zip");
    }

    @Override
    public Result excelImport(HttpServletResponse response, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.endsWith(".zip")){
            return this.importStaffByZipFile(response, file);
        }else {
            return Result.fail("文件类型错误，请上传zip");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    protected Result<String> importStaffByZipFile(HttpServletResponse response, MultipartFile file){
        List<StaffImportResultDTO> staffImportResultDTOS = new ArrayList<>();
        try(ZipParser zipParser = new ZipParser(file.getInputStream(), IMPORT_MAX_SIZE)) {
            Iterator<ZipEntry> xls = zipParser.iterator("xls");
            //导入excel中的数据
            while (xls.hasNext()){
                //需要导入的员工数据
                InputStream in = zipParser.getInputStreamOfEntry(xls.next());
                ImportParams params = new ImportParams();
                params.setSheetNum(1);
                params.setTitleRows(0);
                params.setHeadRows(1);
                List<StaffImportDTO> staffImportDTOList = ExcelUtils.importExcel(in, StaffImportDTO.class, params);
                staffImportResultDTOS = this.importStaffInfoByExcel(staffImportDTOList);
                break;
            }
            //导入图片 图片以 工号.jpg 命名
            importStaffImage(zipParser);
            //导出结果
            ExcelUtils.exportExcel(staffImportResultDTOS, null, "sheet1", StaffImportResultDTO.class, "员工导入结果.xls", response);
            return Result.ok();
        } catch (TooMuchEntryException e) {
            return Result.fail("员工导入错误。");
        } catch (IOException e) {
            return Result.fail("员工导入错误。");
        } catch (Exception e) {
            return Result.fail("员工导入错误。");
        }
    }

    /**
     * 添加员工导入的数据
     * @param staffImportDTOList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    protected List<StaffImportResultDTO> importStaffInfoByExcel(List<StaffImportDTO> staffImportDTOList) {
        List<StaffImportResultDTO> resultList = new ArrayList<>();
        for (StaffImportDTO staffImportDTO : staffImportDTOList) {
            if (checkStaffJobNumber(null, staffImportDTO.getJobNumber())){ //工号已经存在于未退休的员工上
                StaffImportResultDTO staffImportResultDTO = StaffImportResultDTO.builder()
                        .jobNumber(staffImportDTO.getJobNumber())
                        .staffName(staffImportDTO.getName())
                        .status(Constant.FAIL)
                        .reason("工号已存在")
                        .build();
                resultList.add(staffImportResultDTO);
                continue;
            }
            StaffDetailDO staffDetailDO = new StaffDetailDO();
            staffDetailDO.setGender("男".equals(staffImportDTO.getGender()) ? 0 : 1);
            staffDetailMapper.insert(staffDetailDO);
            StaffDO staffDO = new StaffDO();
            staffDO.setStaffDetailId(staffDetailDO.getId());
            staffDO.setName(staffImportDTO.getName());
            staffDO.setJobNumber(staffImportDTO.getJobNumber());
            staffMapper.insert(staffDO);
            //通过部门路径 获取部门id
            Integer departmentId = departmentService.getDepartmentIdByName(staffImportDTO.getDepartmentName());
            //保存员工所属的部门
            RelStaffDepDO relStaffDepDO = new RelStaffDepDO();
            relStaffDepDO.setStaffId(staffDO.getId());
            relStaffDepDO.setDepartmentId(departmentId);
            relStaffDepMapper.insert(relStaffDepDO);
        }
        return resultList;
    }

    private void importStaffImage(ZipParser zipParser){
        List<ZipEntry> entries = new ArrayList<>();
        zipParser.fillZipEntries("jpg", entries);
        for (ZipEntry entry : entries) {
            String imageName = entry.getName();
            //获取图片名称 即工号
            String jobNumber = imageName.substring(0, imageName.lastIndexOf("."));
            //该图片对应工号存在时才添加
            if (checkStaffJobNumber(null, jobNumber)){
                try (InputStream inputStreamOfEntry = zipParser.getInputStreamOfEntry(entry)){
                    //图片存放路径
                    String imagePath = fileUtils.savePic(inputStreamOfEntry, "jpg");
                    String path = null;
                    if (false){ //开启人脸识别
                        //获取人脸特征值
//                        String eigenvalues = organizationProvider.getEigenvalues(imagePath);
                        //获取特征值存放路径
//                        path = organizationProvider.saveEigenvalues(eigenvalues);
                    }
                    //检测该工号对应的所有员工
                    List<Integer> detailIds = staffMapper.selectObjs(new LambdaQueryWrapper<StaffDO>()
                            .select(StaffDO::getStaffDetailId).eq(StaffDO::getJobNumber, jobNumber)).stream().map(it -> (Integer) it) .collect(Collectors.toList());
                    // 在职的唯一员工详情id
                    Integer detailId = (Integer) staffDetailMapper.selectObjs(new LambdaQueryWrapper<StaffDetailDO>()
                            .select(StaffDetailDO::getId)
                            .in(StaffDetailDO::getId, detailIds)
                            .eq(StaffDetailDO::getState, 1))//todo 在职状态字典id
                            .stream().findFirst().orElse(null);
                    if (detailId == null) continue;
                    staffMapper.update(null, new LambdaUpdateWrapper<StaffDO>()
                        .eq(StaffDO::getStaffDetailId, detailId)
                        .set(StaffDO::getIdCardImage, imageName)
                        .set(StaffDO::getEigenvalues, path));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
